<?php

namespace App\Modules\Mq\Services;

use App\Lib\Snowflake\Snowflake;

class PushService {
    const MQ_TASK_DELAYED = 'mq_task_delayed';
    const MQ_TASK_COMMON = 'mq_task_common';
    const MQ_TASK_DELAYED_DEFAULT = 0;
    //请求的地址
    private static $_url;

    /**
     * @desc 推送延时消息
     * @param string $body 消息字符串 一般为json字符串
     * @param string $task 任务名字[common:实时队列,delay:延时队列]
     * @param int $delay 延时时间，毫秒
     * @date 2022-03-08
     * @author  zxiaofee
     * */
    public static function push(array $body = [], string $task = PushService::MQ_TASK_DELAYED, int $delay = PushService::MQ_TASK_DELAYED_DEFAULT): bool
    {
        $snowflake = new Snowflake(mt_rand(0, 3),mt_rand(0, 255));
        $messageId = $snowflake->id();
        $reqUrl = self::_buildReqUrl($messageId, $task, $delay); //构建请求url
        write_log('PushService', 'PushService Request', ['url' => $reqUrl, 'request' => $body]);
        $res = curl_request($reqUrl, $body, 'json', 'POST');
        write_log('PushService', 'PushService Response', ['url' => $reqUrl, 'request' => $body, 'response' => $res]);
        $res = json_decode($res, true);
        if (!empty($res) && isset($res['code']) && $res['code'] == 0) {
            return true;
        }
        return false;
    }

    //初始化参数
    private static function _init() {
        if (self::$_url == null) {
            self::$_url = env('MQ_TASK_URL', 'http://127.0.0.1:7188/task/push');
        }
    }

    //构建请求地址
    private static function _buildReqUrl($messageId, $task, $delay) : string {
        self::_init();
        return self::$_url . '?' . http_build_query([
                'messageId' => $messageId,
                'task' => $task,
                'delay' => $delay,
            ]);
    }

    private static function _httpPost(string $url, string $body, int $timeout = 0) : array {
        $curl = curl_init();
        $timeout = $timeout==0?100:$timeout;
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);    //对HTTPS时 不校验证书
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: text/plain; charset=utf-8',
            'Accept: text/plain'
        ));

        $output = curl_exec($curl);
        $error = curl_error($curl);
        $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        curl_close($curl);

        if($code == '200'){
            // 成功返回
            $ret = json_decode($output, true);
        }else {
            $ret = [
                'code' => -1,
                'msg' => $error
            ];
        }
        return $ret;
    }
}